<template>
  <div>
    <div>
      <el-text type="danger">{{ errorCode }}</el-text>
    </div>
    <el-input
      v-model="code"
      placeholder="请输入纸质代码"
      style="width: 200px"
      @change="cost()"
    ></el-input>
    <el-button @click="cost()">确定</el-button>
    <div style="margin-left: 20px">
      <div>
        <el-text>面纸: {{ mZhiPrice }}</el-text>
      </div>
      <div>
        <el-text>瓦纸1: {{ wZhi1Price }}</el-text>
      </div>
      <div>
        <el-text>芯纸: {{ xZhiPrice }}</el-text>
      </div>
      <div>
        <el-text>瓦纸2: {{ wZhi2Price }}</el-text>
      </div>
      <div>
        <el-text>里纸: {{ lZhiPrice }}</el-text>
      </div>
      <div>
        <el-text>价格: {{ sunPrice }}</el-text>
      </div>
      <div>
        <el-input
          v-model="jiaJia"
          placeholder="请输入加价"
          style="width: 200px"
        ></el-input>
      </div>
      <div>
        <el-text>加价价格: {{ jiaJiaSum }}</el-text>
      </div>
      <div>
        <el-text>含税价格: {{ hanshuiSum }}</el-text>
      </div>
    </div>
  </div>
</template>

<script setup>
import costInfo from "./costInfo";
import { add, floor, toUpper } from "lodash";
let code = ref(""); //纸质代码
let errorCode = ref(""); //错误提示
let lengXing = ref(""); //楞型
let sunPrice = ref(0); //价格
let jiaJia = ref(0); //加价价格
const jiaJiaSum = computed(() => {
  console.log('jiaJiaSum:',jiaJia.value,sunPrice.value);
  return myFixed(add(Number(sunPrice.value), Number(jiaJia.value)),2);
});
const hanshuiSum = computed(() => {
  console.log('hanshuiSum:',jiaJiaSum,sunPrice.value);
  return myFixed(Number(jiaJiaSum.value)*1.06,2)
});

//结果显示
let mZhiPrice = ref("");
let lZhiPrice = ref("");
let wZhi1Price = ref("");
let wZhi2Price = ref("");
let xZhiPrice = ref("");

const lengxingList = ["B", "C", "F", "E", "EB", "BC"]; //楞型
const mianZhiList = ["A", "Q", "T", "W", "D","F"]; //面纸

const cost = () => {
  if (!code.value) {
    errorCode.value = "请输入纸质代码";
    return;
  }
  if (!code.value.includes("/")) {
    errorCode.value = "请输入楞型";
    return;
  }
  const tranCode = toUpper(code.value.trim()).replace(/\s/g, ""); //去除空格转大写的code
  console.log(tranCode);
  let index = tranCode.indexOf("/");

  let lengXing = tranCode.substring(index + 1, code.value.length);
  let zhizhi = tranCode.substring(0, index); //纸质
  let mianZhi = ""; //面纸
  let liZhi = ""; //里纸
  let walengXin = ""; //瓦楞芯纸字符串
  let walengxinList = []; //瓦楞芯纸列表
  let lengXingList = []; //瓦楞列表
  let sumPrice = 0; //总价格

  //拆分纸质

  //获取是否有面纸，确实是二四  还是三五
  const firstChar = tranCode.charAt(0);
  if (mianZhiList.includes(firstChar)) {
    mianZhi = tranCode.substring(0, 2);
  } else {
    mianZhi = "";
  }

  //获取里纸型号
  liZhi = findLizhi(zhizhi);
  //获取瓦楞和夹心
  walengXin = trimStartAndEnd(zhizhi, mianZhi, liZhi);
  //拆分瓦楞和芯纸
  walengxinList = walengXin.split("");
  lengXingList = lengXing.split("");

  //面纸价格
  mZhiPrice.value = getPriceByNoAndType(mianZhi);
  sumPrice = add(sumPrice, getPriceByNoAndType(mianZhi));
  //里纸价格
  lZhiPrice.value = getPriceByNoAndType(liZhi);
  sumPrice = add(sumPrice, getPriceByNoAndType(liZhi));
  //瓦楞芯纸价格
  if (lengXingList.length > 1) {
    walengxinList.forEach((item, index) => {
      if (index == 0) {
        //瓦楞1的价格
        sumPrice = add(sumPrice, getPriceByNoAndType(item, lengXingList[0]));
        wZhi1Price.value = getPriceByNoAndType(item, lengXingList[0]);
      } else if (index == 2) {
        //瓦楞2的价格
        sumPrice = add(sumPrice, getPriceByNoAndType(item, lengXingList[1]));
        wZhi2Price.value = getPriceByNoAndType(item, lengXingList[1]);
      } else {
        //芯纸的价格
        sumPrice = add(sumPrice, getPriceByNoAndType(item));
        xZhiPrice.value = getPriceByNoAndType(item);
      }
    });
  } else {
    //瓦楞1/芯纸的价格
    walengxinList.forEach((item, index) => {
      if(mianZhi){
        sumPrice = add(sumPrice, getPriceByNoAndType(item, lengXingList[0]));
        xZhiPrice.value = getPriceByNoAndType(item, lengXingList[0]);
      }else{
        sumPrice = add(sumPrice, getPriceByNoAndType(item, lengXingList[0],'KT'));
        xZhiPrice.value = getPriceByNoAndType(item, lengXingList[0],'KT');
      }
    });
  }
  console.log("sumPrice:", sumPrice);
  sunPrice.value = floor(sumPrice, 3).toFixed(2);
};
//寻找里纸
const findLizhi = (str) => {
  for (let i = str.length - 1; i >= 0; i--) {
    if (/[a-zA-Z]/.test(str[i])) {
      return str.slice(i);
    }
  }
  return null;
};
//获取瓦楞和夹心
function trimStartAndEnd(str, startStr, endStr) {
  let result = str;
  if (startStr && str.startsWith(startStr)) {
    result = result.slice(startStr.length);
  }
  if (endStr && str.endsWith(endStr)) {
    result = result.slice(0, result.length - endStr.length);
  }
  return result;
}

//获取对应价格
function getPriceByNoAndType(no, type = "",shengChanXian="JS") {
  const item = costInfo.find((cost) => cost.no === no);
  if (!item) {
    return null;
  }
  switch (type.toUpperCase()) {
    case "E":
      if(shengChanXian=='KT'){
        return item.e2Price || item.xiubianPrice;
      }else{
        return item.ePrice || item.xiubianPrice;
      }
    case "B":
      return item.bPrice || item.xiubianPrice;
    case "C":
      return item.cPrice || item.xiubianPrice;
      case "F":
      return item.fPrice || item.xiubianPrice;
    default:
      return item.xiubianPrice;
  }
}
/**
 * 保留小数点几位数, 自动补零, 四舍五入
 * @param num: 数值
 * @param digit: 小数点后位数
 * @returns string
 */
 function myFixed(num, digit) {
  if (Object.is(parseFloat(num), NaN)) {
    return console.log(`传入的值：${num}不是一个数字`);
  }
  num = parseFloat(num);
  return (
    Math.round((num + Number.EPSILON) * Math.pow(10, digit)) /
    Math.pow(10, digit)
  ).toFixed(digit);
}
</script>

<style lang="scss" scoped></style>
